const App = {
  mixins: [Query], //配置文件，路径/utils/mixins.js
  data() {
    return {
      page: {
        value1: "",
        value2: '',
        PageNumber: 1, //页码
        RowAmount: 20,
        Condition: window.btoa(unescape(encodeURIComponent("%%,%%"))),
      },
      type1: [],
      showDialog: false,
      assist_delegate_application_attachment: [],
      fileUrl: '',
      showDialog1: false,
      form: {},
      form1: {
        Application_Main: '',
        Children: {
          assist_delegate_reply_attachment: [],
        }
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    handleClick() {
      if (this.page.activeName == 'ALL') {
        this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent("AND assist_delegate_application.Row_Status in ('0','2')")));
      } else if (this.page.activeName == 'DONE') {
        this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent("AND assist_delegate_application.Row_Status in ('2')")));
      } else if (this.page.activeName == 'UNDONE') {
        this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent("AND assist_delegate_application.Row_Status in ('0')")));
      } else {
        this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent("")));
      }
      this.postgetList(this.page);
    },
    success(row, res) {
      this.form1.Children.assist_delegate_reply_attachment.push({
        uid: row.uid,
        Attachment_URL: res.fileName,
        Attachment_Name: row.name,
        Temp_Path: res.tempPath,
      })
    },
    remove(row) {
      this.form1.Children.assist_delegate_reply_attachment = this.form1.Children.assist_delegate_reply_attachment.filter(i => i.uid != row.uid);
    },
    check() {

      this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%,%" + (this.page.value2 ? this.page.value2 : '') + "%")));
      this.postgetList(this.page);
    },
    getData() {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        method: "post",
        data: {
          Data: 'list_management',
          Condition: `Row_Status <> '1'`,
        },
        success: (res) => {
          this.type1 = res;
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
      });
    },
    handlecl(row) {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        method: "post",
        data: {
          Data: 'assist_delegate_application_attachment',
          Condition: `Application_SerialNumber = '${row.Application_SerialNumber}' AND Row_Status = '0'`,
        },
        success: (res) => {
          this.form = row
          this.assist_delegate_application_attachment = res;
          this.showDialog = true;
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
      });
    },
    preview(row) {
      this.fileUrl = photo + row.Attachment_URL;
      this.showDialog1 = true;
    },
    async download(row) {
      // if (row.Attachment_URL) {
      //   window.location.href = photo + row.Attachment_URL;
      // }

      let apiUrl = photo + row.Attachment_URL;
      let fileName = apiUrl.split('/').pop();

      try {
        const response = await fetch(apiUrl);
        const blob = await response.blob();
        const url = window.URL.createObjectURL(blob);

        const a = document.createElement('a');
        a.href = url;
        a.download = fileName || 'download';
        document.body.appendChild(a);
        a.click();

        // 清理
        setTimeout(() => {
          document.body.removeChild(a);
          window.URL.revokeObjectURL(url);
        }, 100);
      } catch (error) {
        console.error('下载失败:', error);
      }
    },
    ClickConfirm(data) {
      this.$confirm('该操作将此条数据标记已处理,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.showDialog = false;
        let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          method: "post",
          data: {
            Data: TableName,
            Condition: primary_key + "='" + data[primary_key] + "'"
          },
          success: (res) => {
            if (res.length == 1) {
              res[0].Row_Status = '2';
              let lJSONObject_DataSet = new Object();
              let lJSONObject_Table = new Object();
              lJSONObject_Table[TableName] = res;
              lJSONObject_DataSet['Modify'] = lJSONObject_Table;
              this.form1.Application_SerialNumber = this.form.Application_SerialNumber,
                lJSONObject_DataSet['Add'] = {
                  assist_delegate_reply: [this.form1]
                };
              let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
              lJSONObject_DataSet['Add']['base_operation_log'] = [{
                Operation_Type: '0',
                Operation_Path: this.relativePath,
                Operation_Main,
                Operation_Interface: "generalSaveByRequestBody",
                Operation_IP: getIpAddress()
              }]
              submitSave(lJSONObject_DataSet, (res) => {
                if (res.Status == 'success') {
                  this.$message({
                    type: 'success',
                    message: '处理成功'
                  });
                  this.form1 = {
                    Application_Main: '',
                    Children: {
                      assist_delegate_reply_attachment: [],
                    }
                  }
                  this.postgetList(this.page);
                } else {
                  this.$message({
                    type: 'error',
                    message: '操作失败'
                  });
                }
              })
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              });
            }

          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest)
          },
        });
      }).catch(() => { });
    }

  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.component('yl-text-editor', ylTextEditor);
app.component('yl-upload', ylUpload);
app.component('yl-online-preview', ylOnlinePreview);
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
